package com.mhkj.base.itemInfo.building.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.base.itemInfo.building.dao.BuildingDao;
import com.mhkj.base.itemInfo.building.entity.Building;
import com.mhkj.base.itemInfo.building.service.BuildingService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service
public class BuildingServiceImpl extends BaseServiceImpl<Building> implements BuildingService {
	@Resource
	private BuildingDao buildingDao;
	@Resource
	private DictionaryService dictionaryService;

	@Resource
	public void setBuildingDao(BuildingDao BuildingDao) {
		super.setBaseRepository(BuildingDao);
	}

	@Override
	public Pagination<Building> listByPage(Building Building, int pageIndex, int size ) {
		Specification<Building> spec = new Specification<Building>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<Building> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				
				
//				 Join<Object, Object> tSysNation = root.join("tSysNation",JoinType.INNER);
//
//				if (!StringUtils.isEmpty(Building.getCityId())) {
//					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("id"),Building.getCityId()));
//				}
//				
//				if (!StringUtils.isEmpty(Building.getName())) {
//					lstPredicates.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + Building.getName() + "%"));
//				}
//				 
//				if (!StringUtils.isEmpty(Building.getAffiliatedCompanyId())) {
//					lstPredicates.add(criteriaBuilder.equal(root.get("affiliatedCompanyId").as(String.class), Building.getAffiliatedCompanyId()));
//				}
//				
			 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<Building> pageT = buildingDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<Building> pagination = new Pagination<Building>();
		pagination.setTotal(buildingDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

	@Override
	public List<Building> findByCidOrItemName(Long cityId, String itemName, Long provinceId) {
		Specification<Building> spec = new Specification<Building>() {

			/**
			 * 序列号
			 */
			private static final long serialVersionUID = 2102601865556822201L;

			

			@Override
			public Predicate toPredicate(Root<Building> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
			
				
				 Join<Object, Object> phase = root.join("phase",JoinType.INNER);
				 Join<Object, Object> tSysNation = phase.join("tSysNation",JoinType.INNER);
				 Join<Object, Object> item = phase.join("item",JoinType.INNER);

				if (!StringUtils.isEmpty(cityId)) {
					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("id"),cityId));
				}
				if (!StringUtils.isEmpty(provinceId)) {
					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("parentId"),provinceId));
				}
				
				if (!StringUtils.isEmpty(itemName)) {
					lstPredicates.add(criteriaBuilder.like(item.get("name").as(String.class), "%" + itemName + "%"));
				}
			 	
			 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		List<Building> findAll = buildingDao.findAll(spec);
		 
		return findAll;
	}

	@Override
	public List<Building> findBuildByPhaseId(Long phaseId) {
		// TODO Auto-generated method stub
		return buildingDao.findBuildByPhaseId(phaseId);
	}
 
}
